<template>
  <view>
    <view class="" style="padding: 25rpx">
      <view class="" style="margin-bottom: 15rpx"> 当前城市 </view>
      <view class="dis-flex flex-y-center">
        <view
          class="dw dis-flex flex-y-center flex-x-center"
          v-if="address.cityName"
          @click="goBack()"
        >
          <image
            src="@/static/dw-.png"
            mode=""
            style="width: 30rpx; height: 30rpx; vertical-align: baseline"
          >
          </image>
          <view class="dw-text">{{ address.cityName }}</view>
        </view>
        <view class="dw dis-flex flex-y-center flex-x-center" v-else>
          <image
            src="@/static/dw-.png"
            mode=""
            style="width: 30rpx; height: 30rpx; vertical-align: baseline"
          >
          </image>
          <view class="dw-text" style="color: orange">无法获取城市信息</view>
        </view>
        <view class="dis-flex flex-y-center flex-x-center" @click="initLocation(true)">
          <image
            src="@/static/cxdw.png"
            style="width: 30rpx; height: 30rpx; vertical-align: baseline"
            mode=""
          >
          </image>

          <view class="dw-text">重新定位</view>
        </view>
      </view>
    </view>
    <view class="">
      <t-index-address @select="select"></t-index-address>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      address: {
        cityName: '',
        cityCode: ''
      }
    }
  },
  methods: {
    goBack() {
      uni.navigateBack()
    },
    select(e) {
      console.log('selectLocation: ' + JSON.stringify(e))
      uni.dialog.confirm('温馨提示', '确认选该地址【' + e.name + '】？', () => {
        if (e.code.length == 4) {
          e.code = e.code + '00'
        }
        this.address = {
          cityName: e.name,
          cityCode: e.code
        }
        uni.setStorageSync('address', this.address)
        uni.$emit('selectAddress', this.address)
        uni.navigateBack()
      })
    },
    initLocation(reload = false) {
      console.log('reload=' + reload)
      this.address = uni.getStorageSync('address')
      console.log('address=' + JSON.stringify(this.address))
      const firstAddressPer = uni.getStorageSync('firstAddressPer')
      console.log('firstAddressPer=' + firstAddressPer)
      if (!reload) {
        if (this.address && this.address.cityName && firstAddressPer) {
          return
        }
      }
      // #ifndef MP-WEIXIN
      uni.getLocation({
        type: 'gcj02',
        geocode: false,
        isHighAccuracy: true,
        success: (res) => {
          console.log('uni.getLocation: ' + JSON.stringify(res))
          this.decodeLocation(res.latitude, res.longitude)
        },
        fail: function (res) {
          uni.setStorageSync('firstAddressPer', false)
          uni.showToast({
            title: '定位失败，请检查权限和定位是否打开',
            duration: 2000,
            icon: 'none'
          })
        }
      })
      // #endif
      // #ifdef MP-WEIXIN
      wx.authorize({
        scope: 'scope.userFuzzyLocation',
        success: (res) => {
          wx.getFuzzyLocation({
            type: 'gcj02',
            success: (res) => {
              console.log('wx.getFuzzyLocation: ' + JSON.stringify(res))
              this.decodeLocation(res.latitude, res.longitude)
            },
            fail: (err) => {
              uni.setStorageSync('firstAddressPer', false)
              console.log(err)
            }
          })
        }
      })
      // #endif
    },
    decodeLocation(latitude, longitude) {
      this.$u
        .get('/app-api/system/region/location', {
          latitude,
          longitude,
          provider: 'tencent'
        })
        .then((res) => {
          if (!res.data || !res.data.cityCode) {
            uni.setStorageSync('firstAddressPer', false)
            uni.showToast({
              title: '获取城市信息失败',
              duration: 2000,
              icon: 'none'
            })
            return
          }
          uni.setStorageSync('firstAddressPer', true)
          this.address = res.data
          uni.setStorageSync('address', res.data)
        })
    }
  },
  onLoad() {
    this.initLocation()
  }
}
</script>

<style>
.dw {
  display: inline-block;
  margin-right: 20rpx;
  min-width: 200rpx;
  max-width: 400rpx;
  background: #d7f5f1;
  padding: 20rpx;
  border-radius: 10rpx;
}

.dw-text {
  font-size: 30rpx;
  color: #10a28f;
  margin-left: 10rpx;
}
</style>
